#!/bin/bash

# 账号管理器 - 快速启动脚本 (Linux/Mac)

echo "=========================================="
echo "   账号管理器 - 快速启动脚本"
echo "=========================================="
echo ""

# 检查 Node.js
echo "[1/3] 检查环境..."
if ! command -v node &> /dev/null; then
    echo "❌ Node.js 未安装，请先安装 Node.js"
    exit 1
fi

# 检查 PHP
if ! command -v php &> /dev/null; then
    echo "❌ PHP 未安装，请先安装 PHP"
    exit 1
fi

echo "✅ 环境检查通过"
echo ""

# 启动后端
echo "[2/3] 启动后端服务..."
cd backend
php think run -p 8080 &
BACKEND_PID=$!
cd ..
sleep 2
echo "✅ 后端服务已启动 (PID: $BACKEND_PID, 端口: 8080)"
echo ""

# 启动前端
echo "[3/3] 启动前端服务..."
cd frontend
npm run dev &
FRONTEND_PID=$!
cd ..
sleep 2
echo "✅ 前端服务已启动 (PID: $FRONTEND_PID)"
echo ""

echo "=========================================="
echo "   🎉 启动完成！"
echo "=========================================="
echo ""
echo "📌 前端地址: http://localhost:3001"
echo "📌 后端地址: http://localhost:8080"
echo ""
echo "提示: 按 Ctrl+C 停止所有服务"
echo ""

# 等待用户中断
trap "echo ''; echo '正在停止服务...'; kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; echo '✅ 服务已停止'; exit 0" INT TERM

# 保持脚本运行
wait
